public class RemoveLinkedListElements {
    public ListNode removeElements(ListNode head, int val) {
        ListNode virtualNode = new ListNode(-1, head);
        ListNode prev = virtualNode;
        ListNode cur = head;

        while(cur != null) {
            if(cur.val != val) {
                prev = cur;
                cur = cur.next;
            }else {
                prev.next = cur.next;
                cur = cur.next;
            }
        }

        return virtualNode.next;
    }
}
